package org.jeecg.modules.gzgl.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zhaodui.fxy.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.gzgl.api.apientity;
import org.jeecg.modules.gzgl.entity.*;
import org.jeecg.modules.gzgl.service.IFxyGzgl01Service;
import org.jeecg.modules.gzgl.service.IFxyGzgl04Service;
import org.jeecg.modules.gzgl.service.impl.FxyGzgl02ServiceImpl;
import org.jeecg.modules.gzgl.service.impl.FxyGzgl03ServiceImpl;
import org.jeecg.modules.gzgl.service.impl.FxyGzgl05ServiceImpl;
import org.jeecg.modules.gzgl.service.impl.FxyGzgl06ServiceImpl;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.service.impl.SysDataLogServiceImpl;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import sun.util.resources.ar.CurrencyNames_ar_SY;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description: 公证订单主表
 * @Author: zhaodui
 * @Date: 2020-02-06
 * @Version: V1.0
 */
@RestController
@RequestMapping("/gzgl/fxyGzgl01")
@Slf4j
@Api(tags="合同主表")
public class FxyGzgl01Controller extends JeecgController<FxyGzgl01, IFxyGzgl01Service> {
    @Autowired
    private IFxyGzgl01Service fxyGzgl01Service;
    @Autowired
    private SysDataLogServiceImpl sysDataLogService;
    @Autowired
    private FxyGzgl04Controller fxyGzgl04Controller;
    @Autowired
    private IFxyGzgl04Service fxyGzgl04Service;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private FxyGzgl02ServiceImpl fxyGzgl02Service;
    @Autowired
    private FxyGzgl03ServiceImpl fxyGzgl03Service;
    @Autowired
    private FxyGzgl05ServiceImpl fxyGzgl05Service;

    @Autowired
    private FxyGzgl06ServiceImpl fxyGzgl06Service;

    @Value(value = "${baseorg.common.basemobile}")
    private String basemobile;
    @Value(value = "${baseorg.common.dataurl}")
    private String dataurl;
    @Value(value = "${baseorg.common.baseuserid}")
    private String baseuserid;
    @Value(value = "${baseorg.common.processNumber}")
    private String processNumber;
    @Value(value = "${baseorg.common.businessType}")
    private String businessType;
    @Value(value = "${baseorg.common.agreecode}")
    private String agreecode;
    @Value(value = "${baseorg.common.agreename}")
    private String agreename;
    @Value(value = "${baseorg.common.orgcode}")
    private String orgcode;
    @Value(value = "${baseorg.common.htversion}")
    private String htversion;
    @Value(value = "${baseorg.common.appcode}")
    private String appcode;
    private ExecutorService executor = Executors.newCachedThreadPool();


    @Autowired
    private ISysUserService sysUserService;
    /**
     * 分页列表查询
     *
     * @param fxyGzgl01
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<?> queryPageList(FxyGzgl01 fxyGzgl01,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<FxyGzgl01> queryWrapper = QueryGenerator.initQueryWrapper(fxyGzgl01, req.getParameterMap());
        Page<FxyGzgl01> page = new Page<FxyGzgl01>(pageNo, pageSize);
        IPage<FxyGzgl01> pageList = fxyGzgl01Service.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param fxyGzgl01
     * @return
     */
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody FxyGzgl01 fxyGzgl01) {

        fxyGzgl01Service.save(fxyGzgl01);
        Gson gs = new GsonBuilder().serializeNulls().create();
        String datacont = gs.toJson(fxyGzgl01);
        String id = fxyGzgl01.getId();
        sysDataLogService.addDataLog("fxy_gzgl_01", id, datacont);
        return Result.ok("添加成功！");
    }




    public void  regedit(String phone,String name){
        SysUser sysUser2 = sysUserService.getUserByPhone(phone);

        if (sysUser2 == null) {
            SysUser user = new SysUser();
            user.setCreateTime(new Date());// 设置创建时间
            String salt = oConvertUtils.randomGen(8);
            String passwordEncode = PasswordUtil.encrypt(phone, "123456", salt);
            user.setSalt(salt);
            user.setUsername(phone);
            user.setRealname(name);
            user.setPassword(passwordEncode);
//            user.setEmail("");
            user.setPhone(phone);
            user.setStatus(1);
            user.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
            user.setActivitiSync(CommonConstant.ACT_SYNC_1);
            sysUserService.addUserWithRole(user,"ee8626f80f7c2619917b6236f3a7f02b");//默认临时角色 test
        }
    }

    /**
     * 添加
     *
     * @return
     */
    @GetMapping(value = "/getdata")
    @ApiOperation("获取订单")
    public Result<?> getdata(HttpServletRequest request, HttpServletResponse response,
                             @RequestParam(name = "query07", required = false) String query07) {
        ResulFxy resulFxy = new ResulFxy();


        log.info(query07);
        String url = dataurl;
        LinkedHashMap<String, Object> paramMap1 = new LinkedHashMap<String, Object>();
        LinkedHashMap<String, Object> paramMap2 = new LinkedHashMap<String, Object>();
        LinkedHashMap<String, Object> paramMap3 = new LinkedHashMap<String, Object>();
        LinkedHashMap<String, Object> paramMap4 = new LinkedHashMap<String, Object>();
        LinkedHashMap<String, Object> paramMap11 = new LinkedHashMap<String, Object>();
        String outlenth = "20";
        List<apientity.ResultBean> resultres1 =  new ArrayList<>();
        List<apientity.ResultBean> resultres2 = new ArrayList<>();
        List<apientity.ResultBean> resultres3 = new ArrayList<>();
        List<apientity.ResultBean> resultres4 = new ArrayList<>();
        List<apientity.ResultBean> resultres11 = new ArrayList<>();
        String htbl01="/";
        String htbl02="/";
        String htbl03="/";
        String htbl04="/";
        String htbl05="/";
        String htbl06="/";
        String htbl07="/";
        if (StringUtil.isNotEmpty(query07)) {
            deletebypara(query07);
        }
        try {//
            //生成主借人  自然人类型
            paramMap1.put("outlenth", outlenth);
            paramMap1.put("sqltype", "1");
            paramMap1.put("para", query07);
            String result1 = com.xiaoleilu.hutool.http.HttpUtil.post(url, paramMap1);
            apientity apientity1 = JSONHelper.fromJsonToObject(result1, org.jeecg.modules.gzgl.api.apientity.class);
            resultres1 = apientity1.getResult();
        } catch (Exception e) {
        }
        try {//生成 主借人  企业类型
            outlenth = "20";
            paramMap2.put("outlenth", outlenth);
            paramMap2.put("sqltype", "2");
            paramMap2.put("para", query07);
            String result2 = com.xiaoleilu.hutool.http.HttpUtil.post(url, paramMap2);
            apientity apientity2 = JSONHelper.fromJsonToObject(result2, org.jeecg.modules.gzgl.api.apientity.class);
            resultres2 = apientity2.getResult();
        } catch (Exception e) {
        }
        try {// 担保人
            outlenth = "20";
            paramMap3.put("outlenth", outlenth);
            paramMap3.put("sqltype", "3");
            paramMap3.put("para", query07);
            String result3 = com.xiaoleilu.hutool.http.HttpUtil.post(url, paramMap3);
            apientity apientity3 = JSONHelper.fromJsonToObject(result3, org.jeecg.modules.gzgl.api.apientity.class);
            resultres3 = apientity3.getResult();
        } catch (Exception e) {
        }
        try {//生成共借人
            outlenth = "20";
            paramMap4.put("outlenth", outlenth);
            paramMap4.put("sqltype", "4");
            paramMap4.put("para", query07);
            String result4 = com.xiaoleilu.hutool.http.HttpUtil.post(url, paramMap4);
            apientity apientity4 = JSONHelper.fromJsonToObject(result4, org.jeecg.modules.gzgl.api.apientity.class);
            resultres4 = apientity4.getResult();
        } catch (Exception e) {
        }
        try {//生成合同内容
            outlenth = "20";
            paramMap11.put("outlenth", outlenth);
            paramMap11.put("sqltype", "11");
            paramMap11.put("para", query07);
            String result11 = com.xiaoleilu.hutool.http.HttpUtil.post(url, paramMap11);
            apientity apientity11 = JSONHelper.fromJsonToObject(result11, org.jeecg.modules.gzgl.api.apientity.class);
            resultres11 = apientity11.getResult();
        } catch (Exception e) {

        }

        FxyGzgl01 t01 = new FxyGzgl01();//生成01合同主表
        t01.setQuery07(query07);//订单编号
        t01.setQuery08(query07);//合同编号
        t01.setQuery09(agreename);//合同名称
        t01.setQuery01(appcode);
        fxyGzgl01Service.save(t01);

        FxyGzgl04 t04 = new FxyGzgl04();//生成04合同主表
        t04.setQuery07(query07);//订单编号
        t04.setQuery08(query07);//合同编号
        t04.setQuery09(agreename);//合同名称
//        t04.setQuery12("曹二众");//签署人姓名
//        t04.setQuery13("13850081872");//签署人手机号
        t04.setQuery14(baseuserid);//发起人用户ID

        t04.setQuery15(processNumber);//流程号
        t04.setQuery19("0");//是否发送文书
        t04.setQuery20(orgcode);//公证处标识
        t04.setQuery21(businessType);//业务类型
        t04.setQuery22(query07);//文书编号
        t04.setQuery23(agreecode);//模板编号
        t04.setQuery24(agreename);//合同名称
        t04.setQuery01(appcode);

        fxyGzgl04Service.save(t04);

        for (org.jeecg.modules.gzgl.api.apientity.ResultBean t1 : resultres1) {
            FxyGzgl02 t02 = new FxyGzgl02();
            t02.setQuery07(query07);//订单编号
            t02.setQuery08(query07);//合同编号
            t02.setQuery09(agreename);//合同名称
            t02.setQuery12(t1.getOutX5());//个人用户的手机号
            t02.setQuery13(t1.getOutX1());//用户姓名
            t02.setQuery14("111");//个人用户证件类型
            t02.setQuery15(t1.getOutX2());//个人用户证件号码
            t02.setQuery16(orgcode);//公证处标识
//            t02.setQuery17(t2.getOutX1());//邮箱
            t02.setQuery18(t1.getOutX3());//地址
            t02.setQuery19(t1.getOutX1());//第三方ID
            t02.setQuery22("已实名");//已实名
            t02.setQuery26("未签署");//未签署
            t02.setQuery27("自然人主借人");//主借人
            t02.setQuery11("自然人主借人");//主借人
            t02.setQuery38("主借人");
            t02.setQuery01(appcode);
            try{
                regedit(t1.getOutX5(),t1.getOutX1());

            }catch (Exception e){

            }
            fxyGzgl02Service.save(t02);





            //        借款人	htbl02
            htbl02 = t1.getOutX1();//借款人
            htbl03 = t1.getOutX2();//借款人证件号码
            htbl04 = t1.getOutX1();//姓名
            htbl05 = t1.getOutX3();//地址
            htbl06 = t1.getOutX3();//户籍地
            htbl07 = t1.getOutX5();//个人用户的手机号
//        统一社会信用代码（组织机构代码）或身份证号码	htbl03
//        法定代表人/主要负责人	htbl04
//        现居住地	htbl05
//        户籍地	htbl06
//        电话	htbl07
        }

        for (org.jeecg.modules.gzgl.api.apientity.ResultBean t2 : resultres2) {   //企业借款人 同时生成02和03

            FxyGzgl02 t02 = new FxyGzgl02();
            t02.setQuery07(query07);//订单编号
            t02.setQuery08(query07);//合同编号
            t02.setQuery09(agreename);//合同名称
            t02.setQuery12(t2.getOutX4());//个人用户的手机号
            t02.setQuery13(t2.getOutX3());//用户姓名
            t02.setQuery14("111");//个人用户证件类型
            t02.setQuery15(t2.getOutX7());//个人用户证件号码
            t02.setQuery16(orgcode);//公证处标识
//            t02.setQuery17(t2.getOutX1());//邮箱
            t02.setQuery18(t2.getOutX5());//地址
            t02.setQuery19(t2.getOutX2());//第三方ID企业营业执照
            t02.setQuery22("已实名");//已实名
            t02.setQuery26("未签署");//未签署
            t02.setQuery27("企业主借人");//主借人
            t02.setQuery11("企业主借人");//主借人
            t02.setQuery38("主借人");
            t02.setQuery01(appcode);
            try{
                regedit(t2.getOutX4(),t2.getOutX3());

            }catch (Exception e){

            }
            fxyGzgl02Service.save(t02);


            htbl02 = t2.getOutX1();//企业名称
            htbl03 = t2.getOutX2();//借款人证件号码
            htbl04 = t2.getOutX3();//姓名
            htbl05 = t2.getOutX5();//地址
            htbl06 = t2.getOutX5();//户籍地
            htbl07 = t2.getOutX4();//个人用户的手机号

            FxyGzgl03 t03 = new FxyGzgl03();
            t03.setQuery07(query07);//订单编号
            t03.setQuery08(query07);//合同编号
            t03.setQuery09(agreename);//合同名称
            t03.setQuery12(t2.getOutX1());//企业名称
            t03.setQuery13(t2.getOutX2());//企业ID
            t03.setQuery14(orgcode);//公证处标识
            t03.setQuery15(t2.getOutX5());//地址
            t03.setQuery16("111");//个人用户证件类型
//                t03.setQuery17(t2.getOutX4());//邮箱
//            t03.setQuery18(t2.getOutX5());//地址   ok？
            t03.setQuery17(t2.getOutX7());//证件号码
            t03.setQuery18(t2.getOutX4());//法人电话
//            t03.setQuery19(t2.getOutX2());//第三方ID


//            @Excel(name = "企业联系人姓名", width = 15)
//            private String query20;
//            /**企业联系人电话*/
//            @Excel(name = "企业联系人电话", width = 15)
//            private String query21;
//            /**法定代表人姓名*/
//            @Excel(name = "法定代表人姓名", width = 15)
//            private String query22;
//            /**法定代表人手机号*/
//            @Excel(name = "法定代表人手机号", width = 15)
//            private String query23;
//
            t03.setQuery20(t2.getOutX3());//已实名
            t03.setQuery21(t2.getOutX4());//已实名
            t03.setQuery22(t2.getOutX3());//已实名
            t03.setQuery23(t2.getOutX4());//已实名

             t03.setQuery26("已实名");//未签署
            t03.setQuery27("企业借款");//主借人
            t03.setQuery11("企业借款");//主借人
            t02.setQuery38("主借人");
            t03.setQuery01(appcode);

            fxyGzgl03Service.save(t03);

        }

        for (org.jeecg.modules.gzgl.api.apientity.ResultBean t3 : resultres3) {//如果是企业同时到02和03  自然人到02
            String custype = t3.getOutX1();
            if("2".equals(custype)){//自然人
                FxyGzgl02 t02 = new FxyGzgl02();
                t02.setQuery07(query07);//订单编号
                t02.setQuery08(query07);//合同编号
                t02.setQuery09(agreename);//合同名称
                t02.setQuery12(t3.getOutX4());//个人用户的手机号
                t02.setQuery13(t3.getOutX2());//用户姓名
                t02.setQuery14("111");//个人用户证件类型
                t02.setQuery15(t3.getOutX5());//个人用户证件号码
                t02.setQuery16(orgcode);//公证处标识
//                t02.setQuery17(t3.getOutX4());//邮箱
                t02.setQuery18(t3.getOutX3());//地址
                t02.setQuery19(t3.getOutX5());//第三方ID
                t02.setQuery22("已实名");//已实名
                t02.setQuery26("未签署");//未签署
                t02.setQuery27("个人担保人");//主借人
                t02.setQuery11("个人担保人");//主借人
                t02.setQuery38("担保人");
                t02.setQuery01(appcode);
                try{
                    regedit(t3.getOutX4(),t3.getOutX2());

                }catch (Exception e){

                }
                fxyGzgl02Service.save(t02);
            }
            if("1".equals(custype)){//企业
                FxyGzgl02 t02 = new FxyGzgl02();
                t02.setQuery07(query07);//订单编号
                t02.setQuery08(query07);//合同编号
                t02.setQuery09(agreename);//合同名称
                t02.setQuery12(t3.getOutX11());//个人用户的手机号
                t02.setQuery13(t3.getOutX9());//用户姓名
                t02.setQuery14("111");//个人用户证件类型
                t02.setQuery15(t3.getOutX10());//个人用户证件号码
                t02.setQuery16(orgcode);//公证处标识
//                t02.setQuery17(t3.getOutX4());//邮箱
                t02.setQuery18(t3.getOutX7());//地址     OK？

                t02.setQuery19(t3.getOutX8());//第三方ID
                t02.setQuery22("已实名");//已实名
                t02.setQuery26("未签署");//未签署
                t02.setQuery27("企业担保人");//主借人
                t02.setQuery11("企业担保人");//主借人

                t02.setQuery30(t3.getOutX2());//企业名称
                t02.setQuery31(t3.getOutX8());//统一社会信用代码
                t02.setQuery32(t3.getOutX7());//企业地址
                t02.setQuery33(t3.getOutX7());//企业地址
                t02.setQuery34(t3.getOutX10());//企业电话
                t02.setQuery38("担保人");
                t02.setQuery01(appcode);
                try{
                    regedit(t3.getOutX11(),t3.getOutX9());

                }catch (Exception e){

                }
                fxyGzgl02Service.save(t02);


                FxyGzgl03 t03 = new FxyGzgl03();
                t03.setQuery07(query07);//订单编号
                t03.setQuery08(query07);//合同编号
                t03.setQuery09(agreename);//合同名称
                t03.setQuery12(t3.getOutX2());//企业名称
                t03.setQuery13(t3.getOutX8());//企业ID
                t03.setQuery14(orgcode);//公证处标识
                t03.setQuery15(t3.getOutX7());//地址
                t03.setQuery16("111");//个人用户证件类型
                t03.setQuery17(t3.getOutX11());//证件号码
                t03.setQuery18(t3.getOutX10());//法人电话
//                t03.setQuery19(t3.getOutX8());//第三方ID
                t03.setQuery20(t3.getOutX9());//已实名
                t03.setQuery21(t3.getOutX11());//已实名
                t03.setQuery22(t3.getOutX9());//已实名
                t03.setQuery23(t3.getOutX11());//已实名


                 t03.setQuery26("已实名");//未签署
//                t03.setQuery27("企业担保");//主借人
                t03.setQuery11("企业担保");//主借人
                t03.setQuery01(appcode);
                t03.setQuery38("担保人");
                fxyGzgl03Service.save(t03);


            }

        }

        //同时生成合同内容

//        版本号	htbl00
//        合同编号	htbl01
//        借款人	htbl02
//        统一社会信用代码（组织机构代码）或身份证号码	htbl03
//        法定代表人/主要负责人	htbl04
//        现居住地	htbl05
//        户籍地	htbl06
//        电话	htbl07

//        开始日期	htbl20
//        结束日期	htbl21
//        大写金额	htbl22
//        小写金额	htbl23
//        借款利率	htbl24
//        还款方式	htbl25
//        履约保证金比例	htbl26
//        履约保证金大写	htbl27
//        履约保证金小写	htbl28
//        其他特别约定	htbl29
//        合同正本份数	htbl30
//        贷款人份数	htbl31
//        借款人、保证人各执份数	htbl32



String htbl20="/";
String htbl21="/";
String htbl22="/";
String htbl23="/";
String htbl24="/";
String htbl25="/";
String htbl26="/";
String htbl27="/";
String htbl28="/";
String htbl29="/";
String htbl30="/";
String htbl31="/";
String htbl32="/";
        for (org.jeecg.modules.gzgl.api.apientity.ResultBean t11 : resultres11) {
            htbl01  = t11.getOutX4();
            for(int i=0;i<=33;i++){
                if(i==0){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl00");//
                    t05.setQuery16("版本号");//
                    t05.setQuery17(htversion);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==1){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl01");//
                    t05.setQuery16("合同编号");//
                    t05.setQuery17(htbl01);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==2){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl02");//
                    t05.setQuery16("借款人");//
                    t05.setQuery17(htbl02);//
                    fxyGzgl05Service.save(t05);
                }

                if(i==3){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl03");//
                    t05.setQuery16("统一社会信用代码（组织机构代码）或身份证号码");//
                    t05.setQuery17(htbl03);//
                    fxyGzgl05Service.save(t05);
                }

                if(i==4){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl04");//
                    t05.setQuery16("法定代表人/主要负责人");//
                    t05.setQuery17(htbl04);//
                    fxyGzgl05Service.save(t05);
                }

                if(i==5){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl05");//
                    t05.setQuery16("现居住地");//
                    t05.setQuery17(htbl05);//
                    fxyGzgl05Service.save(t05);
                }

                if(i==6){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl06");//
                    t05.setQuery16("户籍地");//
                    t05.setQuery17(htbl06);//
                    fxyGzgl05Service.save(t05);
                }

                if(i==7){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl07");//
                    t05.setQuery16("电话");//
                    t05.setQuery17(htbl07);//
                    fxyGzgl05Service.save(t05);
                }

                //        开始日期	htbl20
//        结束日期	htbl21
//        大写金额	htbl22
//        小写金额	htbl23
//        借款利率	htbl24
//        还款方式	htbl25
//        履约保证金比例	htbl26
//        履约保证金大写	htbl27
//        履约保证金小写	htbl28
//        其他特别约定	htbl29
//        合同正本份数	htbl30
//        贷款人份数	htbl31
//        借款人、保证人各执份数	htbl32
                htbl20 = t11.getOutX5();
                htbl21 = t11.getOutX6();
                htbl22 = Convert.digitToChinese(Integer.parseInt(t11.getOutX7()));
                htbl23 = t11.getOutX7();
                //  这几个字段没有

                htbl24 = t11.getOutX12();//借款利率
                htbl25 = t11.getOutX13();//还款方式
                htbl26 = t11.getOutX14();//履约保证金比例
                htbl27 = Convert.digitToChinese(Integer.parseInt(t11.getOutX15()));//履约保证金大写
                htbl28 = t11.getOutX15();//履约保证金小写
                if(StringUtil.isNotEmpty(t11.getOutX11())){
                    htbl29 = t11.getOutX11();
                }
                htbl30 = t11.getOutX8();
                htbl31 = t11.getOutX9();
                htbl32 = t11.getOutX10();
                if(i==20){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl20");//
                    t05.setQuery16("开始日期");//
                    t05.setQuery17(htbl20);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==21){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl21");//
                    t05.setQuery16("结束日期");//
                    t05.setQuery17(htbl21);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==22){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl22");//
                    t05.setQuery16("大写金额");//
                    t05.setQuery17(htbl22);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==23){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl23");//
                    t05.setQuery16("小写金额");//
                    t05.setQuery17(htbl23);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==24){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl24");//
                    t05.setQuery16("借款利率");//
                    t05.setQuery17(htbl24);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==25){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl25");//
                    t05.setQuery16("还款方式");//
                    t05.setQuery17(htbl25);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==26){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl26");//
                    t05.setQuery16("履约保证金比例");//
                    t05.setQuery17(htbl26);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==27){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl27");//
                    t05.setQuery16("履约保证金大写");//
                    t05.setQuery17(htbl27);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==28){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl28");//
                    t05.setQuery16("履约保证金小写");//
                    t05.setQuery17(htbl28);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==29){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl29");//
                    t05.setQuery16("其他特别约定");//
                    t05.setQuery17(htbl29);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==30){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl30");//
                    t05.setQuery16("合同正本份数");//
                    t05.setQuery17(htbl30);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==31){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl31");//
                    t05.setQuery16("贷款人份数");//
                    t05.setQuery17(htbl31);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==32){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl32");//
                    t05.setQuery16("借款人、保证人各执份数");//
                    t05.setQuery17(htbl32);//
                    fxyGzgl05Service.save(t05);
                }

            }

        }
        FxyGzgl06 t06 = new FxyGzgl06();
        t06.setQuery07(query07);//订单编号
        t06.setQuery08(query07);//合同编号
        t06.setQuery09(agreename);//合同名称
        t06.setQuery17(htbl07);//电话
        t06.setQuery18(htbl02);//姓名
        t06.setQuery11("未付款");//合同名称
        t06.setQuery12("0.01");//合同名称
//        t06.setQuery09(agreename);//合同名称
        t06.setQuery01(appcode);

        fxyGzgl06Service.save(t06);

        resulFxy.setResultCode("success");

        return Result.ok("添加成功！");
    }
    /**
     * 编辑
     *
     * @param fxyGzgl01
     * @return
     */
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody FxyGzgl01 fxyGzgl01) {

        if (StringUtil.isEmpty(fxyGzgl01.getQuery39()) || fxjConstant.iscommitfxy.equals(fxyGzgl01.getQuery39())) {//不提交法信
            fxyGzgl01Service.updateById(fxyGzgl01);
            return Result.ok("编辑成功！");
        }
        commitbatch(fxyGzgl01.getId());//批量提交合同到公证
        fxyGzgl01Service.updateById(fxyGzgl01);
        Gson gs = new GsonBuilder().serializeNulls().create();
        String datacont = gs.toJson(fxyGzgl01);
        String id = fxyGzgl01.getId();
        sysDataLogService.addDataLog("fxy_gzgl_01", id, datacont);
        return Result.ok("编辑成功!");
    }
    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        fxyGzgl01Service.removeById(id);
        return Result.ok("删除成功!");
    }
    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/tuisongchuzheng")
    @ApiOperation("推送出证")
    public Result<?> tuisongchuzheng(@RequestParam(name = "id", required = true) String id) {
        FxyGzgl01 fxyGzgl01 = fxyGzgl01Service.getById(id);
        fxyUser fxyapp = new fxyUser();
        QueryWrapper<FxyGzgl04> queryWrapper4 = new QueryWrapper<FxyGzgl04>();
        queryWrapper4.eq("query07", fxyGzgl01.getQuery07());
        List<FxyGzgl04> list04 =   fxyGzgl04Service.list(queryWrapper4);
        fxyapp.setSerialNumber(fxyGzgl01.getQuery07());
        fxyapp.setUserId(list04.get(0).getQuery14());
        fxyapp.setCaseNumber(list04.get(0).getQuery16());
        try{
            String res =  fxyUtil.applyCert(appcode,redisUtil,fxyapp);
            fxyGzgl01.setQuery11(res);
            fxyGzgl01Service.updateById(fxyGzgl01);
            return Result.ok("推送成功");
        }catch (BaseBusinessException e){
             return  Result.error(e.getMessage());
        }
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/zhuangtaichaxun")
    @ApiOperation("公证书状态查询")
    public Result<?> zhuangtaichaxun(@RequestParam(name = "id", required = true) String id) {
        FxyGzgl01 fxyGzgl01 = fxyGzgl01Service.getById(id);
        fxyUser fxyapp = new fxyUser();
        fxyapp.setSerialNumber(fxyGzgl01.getQuery07());
        try{
            String res =  fxyUtil.searchBidOrderStatus(appcode,redisUtil,fxyapp);
            fxyGzgl01.setQuery10(res);
            fxyGzgl01Service.updateById(fxyGzgl01);
            return Result.ok("状态查询成功"+res);
        }catch (BaseBusinessException e){
            return  Result.error(e.getMessage());
        }
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/xiazaizhengshu")
    @ApiOperation("下载公证书")
    public Result<?> xiazaizhengshu(@RequestParam(name = "id", required = true) String id) {
        FxyGzgl01 fxyGzgl01 = fxyGzgl01Service.getById(id);
        fxyUser fxyapp = new fxyUser();
        fxyapp.setSerialNumber(fxyGzgl01.getQuery07());
        try{
            String res =  fxyUtil.bidorderDownLoad(appcode,redisUtil,fxyapp);
            fxyGzgl01.setQuery10(res);
            fxyGzgl01Service.updateById(fxyGzgl01);
            return Result.ok("状态查询成功");
        }catch (BaseBusinessException e){
            return  Result.error(e.getMessage());
        }
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/xiazaihetong")
    @ApiOperation("下载文书")
    public Result<?> xiazaihetong(@RequestParam(name = "id", required = true) String id) {
        FxyGzgl01 fxyGzgl01 = fxyGzgl01Service.getById(id);
        fxyUser fxyapp = new fxyUser();
        QueryWrapper<FxyGzgl04> queryWrapper4 = new QueryWrapper<FxyGzgl04>();
        queryWrapper4.eq("query07", fxyGzgl01.getQuery07());
        List<FxyGzgl04> list04 =   fxyGzgl04Service.list(queryWrapper4);
        fxyapp.setCaseNumber(list04.get(0).getQuery16());
        try{
            String res =  fxyUtil.downLoadagreement(appcode,redisUtil,fxyapp);
            return Result.ok(res);
        }catch (BaseBusinessException e){
            return  Result.error(e.getMessage());
        }
    }
    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.fxyGzgl01Service.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        FxyGzgl01 fxyGzgl01 = fxyGzgl01Service.getById(id);
        if (fxyGzgl01 == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(fxyGzgl01);
    }


    public void deletebypara(String para) {
        QueryWrapper<FxyGzgl01> queryWrapper1 = new QueryWrapper<FxyGzgl01>();
        queryWrapper1.eq("query07", para);
        fxyGzgl01Service.remove(queryWrapper1);

        QueryWrapper<FxyGzgl02> queryWrapper2 = new QueryWrapper<FxyGzgl02>();
        queryWrapper2.eq("query07", para);
        fxyGzgl02Service.remove(queryWrapper2);

        QueryWrapper<FxyGzgl03> queryWrapper3 = new QueryWrapper<FxyGzgl03>();
        queryWrapper3.eq("query07", para);
        fxyGzgl03Service.remove(queryWrapper3);

        QueryWrapper<FxyGzgl04> queryWrapper4 = new QueryWrapper<FxyGzgl04>();
        queryWrapper4.eq("query07", para);
        fxyGzgl04Service.remove(queryWrapper4);

        QueryWrapper<FxyGzgl05> queryWrapper5 = new QueryWrapper<FxyGzgl05>();
        queryWrapper5.eq("query07", para);
        fxyGzgl05Service.remove(queryWrapper5);

        QueryWrapper<FxyGzgl06> queryWrapper6 = new QueryWrapper<FxyGzgl06>();
        queryWrapper6.eq("query07", para);
        fxyGzgl06Service.remove(queryWrapper6);

//        fxyGzgl01Service.deletebyquery07(para);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param fxyGzgl01
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FxyGzgl01 fxyGzgl01) {
        return super.exportXls(request, fxyGzgl01, FxyGzgl01.class, "公证订单主表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FxyGzgl01.class);
    }

    public void commitbatch(String id) {
        FxyGzgl01 fxyGzgl01 = fxyGzgl01Service.getById(id);
        List<FxyGzgl04> list04 = fxyGzgl04Controller.get04by07(fxyGzgl01.getQuery07());
        FxyGzgl04 fxyGzgl04 = list04.get(0);
        try {
            String appCode = fxyGzgl01.getQuery01();
            fxyUser fxyUser = new fxyUser();

            fxyUser.setCaseNumber(fxyGzgl04.getQuery16());
            fxyUser.setUserId(fxyGzgl04.getQuery14());
            fxyUser.setOrgId(fxyGzgl04.getQuery20());
            fxyUser.setBusinessType(fxyGzgl04.getQuery21());
            fxyUser.setProcessNumber(fxyGzgl04.getQuery15());
            fxyUser.setSendAgreement(fxyGzgl04.getQuery19());
            List<fxyUser> fxyUserList = new ArrayList<>();
            String caseNumber = "";
            for (FxyGzgl04 tp : list04) {
                if(!StringUtil.isEmpty(tp.getQuery16())){//已经生成的就不需要再次生成
                    caseNumber = tp.getQuery16();
                    continue;
                }
                fxyUser fxy = new fxyUser();
                 fxy.setAgreementCode(tp.getQuery22());
                fxy.setTemplateCode(tp.getQuery23());
                fxy.setAgreementName(tp.getQuery24());
                List<FxyGzgl05> fxyGzgl05List = new ArrayList<>();
                List<FxyGzgl02> fxyGzgl02List = new ArrayList<>();
                List<FxyGzgl02> fxyGzgl02dbrList = new ArrayList<>();

                List<FxyGzgl03> fxyGzgl03List = new ArrayList<>();
                //如果数据为空则从系统获取
                //如果数据为空则从系统获取
                if (fxyGzgl02List == null || fxyGzgl02List.size() == 0) {
                      fxyGzgl02List = fxyGzgl04Controller.get02by0708(tp.getQuery07(), tp.getQuery08());
                }
                if (fxyGzgl03List == null || fxyGzgl03List.size() == 0) {
                    fxyGzgl03List = fxyGzgl04Controller.get03by0708(tp.getQuery07(), tp.getQuery08());
                }
                                       if (fxyGzgl05List == null || fxyGzgl05List.size() == 0) {
                    fxyGzgl05List = fxyGzgl04Controller.get05by0708(tp.getQuery07(), tp.getQuery08());
                }
                List<conmap> conmapList = new ArrayList<conmap>();
                if (fxyGzgl05List != null || fxyGzgl05List.size() > 0) {

                    for (FxyGzgl05 t : fxyGzgl05List) {
                        conmap con = new conmap();
                        con.setConKey(t.getQuery15());
                        con.setConValue(t.getQuery17());
                        conmapList.add(con);
                    }
                }
                //增加模板
                for(FxyGzgl02 tdbr: fxyGzgl02List){
                    if("个人担保人".equals(tdbr.getQuery11())||"企业担保人".equals(tdbr.getQuery11())){
                        fxyGzgl02dbrList.add(tdbr);
                    }

                }
                conmap con = new conmap();
                con.setConKey("userInfosTpl");
                JSONArray users = new JSONArray();
                JSONObject user = new JSONObject();
                String convaluedef = "[[\"\\ \",\"\\ \",\"\\ \",\"\\ \",\"\\ \",\"\\ \"] ]";
                if(fxyGzgl02dbrList.size()==0){
                    con.setConValue(convaluedef);
                }else{
//                    保保证证人人：：0 统一社会信用代码（组织机构代码）或身份证号码：1 法定代表人/主要负责人：2 现居住地：2 户籍地： 2 电话： 2
//                    con.setConValue("[[\"0\",\"1\",\"2\",\"2\",\"2\",\"2\"],[\"00\",\"11\",\"22\",\"2\",\"2\",\"2\"]]");
                    int i = 0;
                    String convalue = "";
                    for(FxyGzgl02 tdbradd: fxyGzgl02dbrList){
                        String i1 = "\\";
                        String i2 = "\\";
                        String i3 = "\\ ";
                        String i4 = "\\";
                        String i5 = "\\";
                        String i6 =  "\\";
                        if("个人担保人".equals(tdbradd.getQuery11()) ){
                            i1 = tdbradd.getQuery13();
                            i2 = tdbradd.getQuery15();
                            i3 = tdbradd.getQuery13();
                            i4 = tdbradd.getQuery18();
                            i5 = tdbradd.getQuery18();
                            i6 = tdbradd.getQuery12();
                         }else{
                            QueryWrapper<FxyGzgl03> queryWrapper3 = new QueryWrapper<FxyGzgl03>();
                            queryWrapper3.eq("query13", tdbradd.getQuery19());
                            queryWrapper3.eq("query07", tdbradd.getQuery07());
                            List<FxyGzgl03> list03 = fxyGzgl03Service.list(queryWrapper3);
                            i1 = tdbradd.getQuery13();
                            try{
                                i2 = list03.get(0).getQuery13() ;
                            }catch (Exception e){

                            }
                            i3 = tdbradd.getQuery13();
                            i4 = tdbradd.getQuery18();
                            i5 = tdbradd.getQuery18();
                            i6 = tdbradd.getQuery12();
                        }

                        i++;
                        if(i==1){
                            convalue  = "[[\"" +i1+
                                    "\",\"" +i2+
                                    "\",\"" +i3+
                                    "\",\"" +i4+
                                    "\",\"" +i5+
                                    "\",\"" +i6+
                                    "\"]";
                        }else{
                            convalue  = convalue +  ",[\"" +i1+
                                    "\",\"" +i2+
                                    "\",\"" +i3+
                                    "\",\"" +i4+
                                    "\",\"" +i5+
                                    "\",\"" +i6+
                                    "\"]";
                        }

                    }
                    convalue  = convalue +  "\"]";
                    con.setConValue(convalue);
                    //完成后
                }
//                user.
//                con.setConValue(t.getQuery17());
                conmapList.add(con);
                fxy.setConmapList(conmapList);



                String userids = "";
                String signs = "";
                List<fxysign> listsign = new ArrayList<>();
                if (fxyGzgl02List != null || fxyGzgl02List.size() > 0) {
                    for (FxyGzgl02 t2 : fxyGzgl02List) {
                         if("个人担保人".equals(t2.getQuery11())||"自然人主借人".equals(t2.getQuery11())) {
                             fxysign fxysign = new fxysign();
                             if (StringUtil.isNotEmpty(t2.getQuery20())) {
                                 fxysign.setUserId(t2.getQuery20());
                                 fxysign.setUserName(t2.getQuery38());
                             } else {
                                 //跳出
                             }
                             listsign.add(fxysign);
                         }else{
                         }
                    }
                }
                if (fxyGzgl03List != null || fxyGzgl03List.size() > 0) {
                    for (FxyGzgl03 t3 : fxyGzgl03List) {
//                        userids = userids + t3.getQuery24() + ",";
//                        signs = signs + t3.getQuery12() + ",";
                        fxysign fxysign = new fxysign();
                        if(StringUtil.isNotEmpty(t3.getQuery24())){
                            fxysign.setUserId(t3.getQuery24());
                            fxysign.setCompanyName(t3.getQuery38());
                        }else{
//                            fxysign.setUserName(t3.getQuery22());
//                            fxysign.setCardNo(t3.getQuery17());
//                            fxysign.setMobile(t3.getQuery18());
//                            fxysign.setCompanyName(t3.getQuery12());
//                            fxysign.setContactsName(t3.getQuery20());
//                            fxysign.setContactsPhone(t3.getQuery21());
//                            fxysign.setBusinessLicenseId(t3.getQuery13());
                        }
//                        userids = userids + t2.getQuery20() + ",";
//                        signs = signs + t2.getQuery13() + ",";
                        listsign.add(fxysign);
                    }
                }
                fxy.setUserIds(userids);
                fxy.setSignatorys(signs);
                fxy.setSignList(listsign);
                fxyUserList.add(fxy);
            }
            agreementres res = fxyUtil.addAgreementByProcessNumberreturnobjectbatch(appCode, redisUtil, fxyUser, fxyUserList);
            if (!StringUtil.isEmpty(res.getResultCode())) {
                fxyGzgl04.setQuery16(res.getResultData().getCaseNumber());//案件编号
                fxyGzgl04.setQuery17(res.getResultData().getWritInfos().getWritNumber());//批次编号
                fxyGzgl04.setQuery18(res.getResultData().getWritInfos().getAgreementInfos().get(0).getStorageNo());//存证编号
                List<agreementres.ResultDataBean.WritInfosBean.AgreementInfosBean> writInfos = res.getResultData().getWritInfos().getAgreementInfos();
                for (FxyGzgl04 tpsave : list04) {
                    FxyGzgl04 fxyGzgl04save = new FxyGzgl04();
                    BeanUtils.copyProperties(tpsave, fxyGzgl04save);
                    fxyGzgl04save.setQuery16(fxyGzgl04.getQuery16());//案件编号
                    fxyGzgl04save.setQuery17(res.getResultData().getWritInfos().getWritNumber());//案件编号
                    String wsbh = fxyGzgl04save.getQuery22();//文书编号
                    for (agreementres.ResultDataBean.WritInfosBean.AgreementInfosBean ta : writInfos) {
                        if (ta.getAgreementCode().equals(wsbh)) {
                            fxyGzgl04save.setQuery18(ta.getStorageNo());//存证编号
                        }
                    }
                    fxyGzgl04Service.updateById(fxyGzgl04save);
                }
            }
        } catch (BaseBusinessException e) {

        }
    }

}
